﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Security.Cryptography;

namespace SMSender
{
    public partial class Form1 : Form
    {
        private string HOST = "";
        private string MO = "";
        private string BV = "";
        private string URL_SMS = "";
        private string URL_EXIT = "";

        private string MYACCOUNT = "";
        private string MYPASSWORD = "";

        private bool login = false;
        private bool getBvAndMo = false;
        private bool getSMSUrl = false;
        private bool trySendSMS = false;
        private bool checkSendSMS = false;
        private bool logout = false;

        public struct Friend
        {
            public string number;
            public string name;
        }
        List<Friend> FRIENDLIST = new List<Friend>();

        private delegate void FlushClient(); //代理
        Thread thread = null;


        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

            button_ImportList.Enabled = false;
            ZHUCE.REGISTERED = ZHUCE.Judge_Whether_Registered();
            if (ZHUCE.REGISTERED)
            {
                ZHUCE.VisitCount = 10000000;
                RegeToolStripMenuItem.Text = "关于";
                RegeToolStripMenuItem.ForeColor = System.Drawing.Color.Black;
            }
            else
            {
                ZHUCE.VisitCount = 10;
            }
        }

        #region webbrowser
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //logger_list(this.webBrowser1.Url.ToString());           //记录
            urlComboBox.Text = this.webBrowser1.Url.AbsoluteUri;    //地址栏
            //this.Text = webBrowser1.DocumentTitle;                  //标量
        }
        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }
        #endregion

        #region thread
        private void CrossThreadFlush()
        {
            while (true)
            {
                Random randomNumber = new Random();
                Thread.Sleep(randomNumber.Next(3000, 6000));
                ThreadFunction();
            }
        }
        private void ThreadFunction()
        {
            if (this.dataGridView_QQFriendList.InvokeRequired || this.listBox1.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(ThreadFunction);
                this.Invoke(fc); //通过代理调用刷新方法
            }
            else
            {
                if (!login)
                {
                    login = startLogin();
                    if (!login)
                    {
                        startLogout();
                    }
                }
                else if (!getSMSUrl)
                {
                    getSMSUrl = checkLoginANDpickUpSMSUrl(this.webBrowser1);
                    if (getSMSUrl)
                    {
                        //http://m.mail.10086.cn/bv12/sendsms.html?&mo=wTaorpwZBlN4K6zfBEJrWLLiB2aFmksc&vn=280&cmd=40
                        //http://m.mail.10086.cn/bv12/sendsms.html?&mo=5fj_PcQOvdX1TZ7yOgRjTnKh85FVsbnV&vn=280&cmd=40
                        string SMSUrl = "http://" + HOST + "/" + BV + "/" + URL_SMS;
                        this.webBrowser1.Navigate(SMSUrl.Replace("&amp;", "&"));
                    }
                }
                else if (!trySendSMS)
                {
                    trySendSMS = tryToSendSMS(this.webBrowser1);
                    if (trySendSMS)
                    {
                        checkSendSMS = checkSendResult();
                    }
                }
                else if (!checkSendSMS)
                {
                    //logout = checkLogout();
                }
            }
        }
        #endregion

        #region record
        private void logger_list(string content)
        {
            DateTime currentTime = DateTime.Now;
            string current_record = currentTime.ToString("T") + " " + content;
            listBox1.Items.Add(current_record);
            listBox1.TopIndex = this.listBox1.Items.Count - (int)(this.listBox1.Height / this.listBox1.ItemHeight);
        }
        private void logger_file(string content)
        {
            string fname = "logfile.txt";
            FileInfo finfo = new FileInfo(fname);
            if (finfo.Exists && finfo.Length > 512000)
            {
                finfo.Delete();
            }
            using (FileStream fs = finfo.OpenWrite())
            {
                DateTime currentTime = DateTime.Now;
                string current_record = currentTime.ToString("T") + " " + content;
                StreamWriter w = new StreamWriter(fs);
                w.BaseStream.Seek(0, SeekOrigin.End);//设置写数据流的起始位置为文件流的末尾  
                w.Write(current_record + "\r\n");  //写入日志内容并换行  
                w.Flush();
                w.Close();  //关闭写数据流  
            }
        }
        private void logger_all(string log)
        {
            logger_list(log);
            logger_file(log);
        }
        #endregion

        #region regedit
        private void RegeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 aboutBox = new AboutBox1();
            aboutBox.ShowDialog();
        }
        #endregion

        #region getQFriendList
        private void button_Start_Click(object sender, EventArgs e)
        {
            login = false;
            getBvAndMo = false;
            getSMSUrl = false;
            trySendSMS = false;
            logout = false;
            FRIENDLIST.Clear();
            dataGridView_QQFriendList.Rows.Clear();

            MYACCOUNT = textBox_Account.Text;
            MYPASSWORD = textBox_Password.Text;

            if (MYACCOUNT == "" || MYPASSWORD == "")
            {
                MessageBox.Show("用户名或密码不能为空！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            logger_list("开始与服务器通讯");
            string qzoneurl = "http://wapmail.10086.cn/";
            webBrowser1.Navigate(qzoneurl);
            this.button_Start.Enabled = false;

            thread = new Thread(CrossThreadFlush);
            thread.IsBackground = true;
            thread.Start();
        }
        private bool startLogin()
        {
            logger_list("开始查找登录入口");
            HtmlElement ur = webBrowser1.Document.All["ur"];
            HtmlElement pw = webBrowser1.Document.All["pw"];
            //HtmlElement btlogin = webBrowser1.Document.All["btlogin"];  //name="btlogin" class="btn1" id="submitBtn"
            if (ur == null || pw == null)
            {
                logger_list("无法找到登录入口");
                return false;
            }
            else
            {
                //uin.SetAttribute("value", "118134555");
                ur.InnerText = MYACCOUNT; //设置网页元素的另一种方法
                pw.SetAttribute("value", MYPASSWORD);
                webBrowser1.Document.InvokeScript("login1");
                logger_list("开始尝试登录");
                return true;
            }
        }
        private bool pickUpHOSTAndBV(Uri url, out string host, out string bv)
        {
            logger_list("开始协商通讯参数");
            host = url.Host;
            string regBv = "/(bv\\d+)/";
            //string regMo = "mo=(.+?)&";
            if (url.AbsoluteUri.Contains("bv"))
            {
                Match matchBv = Regex.Match(url.AbsoluteUri, regBv);
                bv = matchBv.Groups[1].ToString();
                if (bv != "" && host != "")
                {
                    logger_list("协商通讯参数成功");
                    return true;
                }
            }
            bv = "";
            host = "";
            logger_list("协商通讯参数失败");
            return false;
        }
        private bool checkLoginANDpickUpSMSUrl(WebBrowser myWebBrowser)
        {
            HtmlDocument elements = myWebBrowser.Document;
            //logger_list("开始校验登录");
            if (myWebBrowser.Url.ToString().Contains("error"))
            {
                logger_list("登录失败！用户名或密码错误？");

                //失败后禁止再次尝试
                login = true;
                getBvAndMo = true;
                getSMSUrl = true;
                trySendSMS = true;
                logout = true;

                return true;
            }

            getBvAndMo = pickUpHOSTAndBV(this.webBrowser1.Url, out HOST, out BV);
            if (!getBvAndMo)
            {
                return false;
            }

            logger_list("登录成功");
            login = true;
            logger_list("开始启动获取请求");
            
            foreach (HtmlElement em in elements.All) 
            {
                string str = em.Name;
                string id = em.Id;
                string htmlinner = em.InnerHtml;
                string htmlouter = em.OuterHtml;

                if ((htmlinner != null) && htmlinner.StartsWith("<A id=id_inbox") && (htmlinner.Contains("sendsms")))
                {
                    string[] gridObjectList = Regex.Split(htmlinner, "</A><A", RegexOptions.IgnoreCase);
                    for (int i = 0; i < gridObjectList.Length; i++)
                    {
                        //logger_list(gridObjectList[i]);
                        Match UrlPath = Regex.Match(gridObjectList[i], "href=\"(.+?)\"");
                        if (gridObjectList[i].Contains("sendsms") && UrlPath.Success)
                        {
                            URL_SMS = UrlPath.Groups[1].ToString();
                            logger_list("启动获取请求成功");
                            return true;
                        }
                    }
                }
            }
            logger_list("启动获取请求失败!");
            return false;
            
        }
        private bool tryToSendSMS(WebBrowser myWebBrowser)
        {
            logger_list("开始获取发送接口");
            HtmlElement receiver = webBrowser1.Document.All["reciever"];
            HtmlElement content = webBrowser1.Document.All["content"];
            if (receiver == null || content == null)
            {
                logger_list("无法获取发送接口");
                return false;
            }
            else
            {
                //uin.SetAttribute("value", "118134555");
                receiver.InnerText = "15999529749"; //设置网页元素的另一种方法
                content.SetAttribute("value", "test in software2");
                //Thread.Sleep(500000);
                //webBrowser1.Document.InvokeScript("sms_send");
                logger_list("开始尝试发送");
                return true;
            }
        }
        private bool checkSendResult()
        {
            int waitTimes = 0;
            while (!this.webBrowser1.Url.ToString().Contains("smsok"))
            {
                if (waitTimes >= 10)
                {
                    logger_list("发送失败！?");
                    return false;
                }
                Thread.Sleep(3000);
                waitTimes++;
            }
            logger_list("发送成功！");
            return true;
           
        }
        private void startLogout()
        {
            getBvAndMo = pickUpHOSTAndBV(this.webBrowser1.Url, out HOST, out BV);
            if (!getBvAndMo)
            {
                logger_list("无法全身而退！？"); ;
            }
            URL_EXIT = "/cgi-bin/logout?sid=" + MO;
            string exitUrl = "http://" + HOST + URL_EXIT;
            logger_list("开始退出");
            this.webBrowser1.Navigate(exitUrl);
        }
        private bool checkLogout()
        {
            if (this.webBrowser1.Url.ToString().Contains("logout") && this.webBrowser1.Url.ToString().Contains("loginpage"))
            {
                logger_list("退出成功");
                return true;
            }
            return false;
        }
        private void insertFriendToList()
        {
            for (int i = 0; i < FRIENDLIST.Count; i++)
            {
                DataGridViewRow row_friend = new DataGridViewRow();
                DataGridViewTextBoxCell cell_number = new DataGridViewTextBoxCell();
                cell_number.Value = FRIENDLIST[i].number;
                row_friend.Cells.Add(cell_number);

                DataGridViewTextBoxCell cell_name = new DataGridViewTextBoxCell();
                cell_name.Value = FRIENDLIST[i].name;
                row_friend.Cells.Add(cell_name);

                string tabNumber = (FRIENDLIST[i].number.ToString().Length < 9) ? ("\t\t") : ("\t");
                //logger_list("QQ号码: " + FRIENDLIST[i].number + tabNumber + "备注姓名: " + FRIENDLIST[i].name);

                dataGridView_QQFriendList.Rows.Add(row_friend);
            }
        }

        private void button_ImportList_Click(object sender, EventArgs e)
        {
            string fname = "qqfriend.txt";
            FileInfo finfo = new FileInfo(fname);
            if (finfo.Exists)
            {
                finfo.Delete();
            }

            for (int i = 0; i < FRIENDLIST.Count; i++)
            {
                if (!ZHUCE.REGISTERED && i > 10)
                {
                    break;
                }
                else
                {
                    string tabNumber = (FRIENDLIST[i].number.ToString().Length < 8) ? ("\t\t") : ("\t");
                    string content = "QQ号码: " + FRIENDLIST[i].number + tabNumber + "备注姓名: " + FRIENDLIST[i].name;
                    using (FileStream fs = finfo.OpenWrite())
                    {
                        StreamWriter w = new StreamWriter(fs);
                        w.BaseStream.Seek(0, SeekOrigin.End);//设置写数据流的起始位置为文件流的末尾
                        if (i == 0)
                        {
                            DateTime currentTime = DateTime.Now;
                            if (ZHUCE.REGISTERED)
                            {
                                content = currentTime.ToString("G") + "\r\n\r\nQQ号码" + textBox_Account.Text + "的好友列表如下：\r\n===================\r\n" + content;
                            }
                            else
                            {
                                content = currentTime.ToString("G") + "\r\n未注册版本，最多导出10条\r\nQQ号码" + textBox_Account.Text + "的好友列表如下：\r\n===================\r\n" + content;
                            }
                            
                        }
                        w.Write(content + "\r\n");  //写入日志内容并换行  
                        w.Flush();
                        w.Close();  //关闭写数据流  
                    }
                }
            }

            System.Diagnostics.Process.Start("notepad.exe", "qqfriend.txt");
            button_ImportList.Enabled = false;

        }
        #endregion

        private void dataGridView_QQFriendList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dw = (DataGridView)sender;
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dw.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
            dw.RowHeadersDefaultCellStyle.Font,rectangle,
            dw.RowHeadersDefaultCellStyle.ForeColor,
            TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }


    }
}
